package com.ciaojian.store.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ciaojian.core.enums.business.ScheduleTypeEnum;
import com.ciaojian.core.enums.business.WorkerApplyStatusEnum;
import com.ciaojian.core.mapper.WorkerApplyMapper;
import com.ciaojian.core.model.LeaveDetail;
import com.ciaojian.core.model.Scheduling;
import com.ciaojian.core.model.WorkerApply;
import com.ciaojian.core.model.dto.WorkerApplyDTO;
import com.ciaojian.core.service.WorkerApplyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Atlakyn
 */
@Service
public class WorkerApplyServiceImpl extends WorkerApplyService<WorkerApplyMapper, WorkerApply> {
    @Resource
    private LeaveDetailServiceImpl leaveDetailService;
    @Resource
    private SchedulingServiceImpl schedulingService;

    /*
     * ======================================= 查询 =======================================
     */

    /**
     * 按状态查询发型师申请
     *
     * @param params 参数
     * @param page   分页插件
     * @return 分页数据
     */
    public Page<Map<String, Object>> listByStatus(WorkerApplyDTO params,
                                                  Page<Map<String, Object>> page) {
        return this.baseMapper.listContainLeaveDetail(page, params);
    }

    /*
     * ======================================= 修改 =======================================
     */

    /**
     * 审核请假申请
     */
    public void auditLeave(WorkerApply workerApply,
                           WorkerApplyStatusEnum status,
                           String disallowReason) {
        switch (status) {
            // 审核通过, 去除排班
            case PASS:
                workerApply.setStatus(status);
                workerApply.updateById();
                // 请假详情
                LeaveDetail detail = leaveDetailService.getByApplyId(workerApply.getId());
                // 查询发型师指定时间段内的排班
                List<Scheduling> scheduleList = schedulingService.listByWorkerIdAndBetweenDate(
                        workerApply.getWorkerId(), detail.getBeginDate(), detail.getEndDate());
                // 设置成请假
                scheduleList.forEach(schedule -> schedule.setDroster("请假"));
                scheduleList.forEach(schedule -> schedule.setRosterId(1));

                // 是否请假半天
                if (ObjectUtil.isNotNull(detail.getHalf())) {
                    scheduleList.forEach(schedule -> schedule.setHalf(detail.getHalf()));
                }

                // 排班安排请假不变
                scheduleList.forEach(schedule -> {
                    if(schedule.getDroster().equals("请假") && schedule.getType()==ScheduleTypeEnum.LEAVE){
                        schedule.setStatus(WorkerApplyStatusEnum.WAIT);
                    }
                });

                // 设置成请假
                scheduleList.forEach(schedule -> schedule.setType(ScheduleTypeEnum.LEAVE));




                schedulingService.saveOrUpdateBatch(scheduleList);
                break;
            // 审核驳回
            case DISMISSED:
                workerApply.setStatus(status);
                workerApply.setDisallowReason(disallowReason);
                workerApply.updateById();
                break;
            default:
                break;
        }

    }


}
